<html>
<head><meta charset="utf-8"><title>why does Any need `: &#x27;static`? · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/why.20does.20Any.20need.20.60.3A.20&#x27;static.60.3F.html">why does Any need `: &#x27;static`?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="178249474"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/why%20does%20Any%20need%20%60%3A%20%27static%60%3F/near/178249474" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/why.20does.20Any.20need.20.60.3A.20&#x27;static.60.3F.html#178249474">(Oct 16 2019 at 00:58)</a>:</h4>
<p>I know this is a restriction, but I've never actually thought about <em>why</em>. Is it because <code>Concrete&lt;'a&gt;</code> and <code>Concrete&lt;'static&gt;</code> become the same trait object, and if we allowed downcasting <code>Concrete&lt;'a&gt;</code> through <code>TraitObject + 'a</code> to <code>Concrete&lt;'static&gt;</code> we'd have memory unsafety?</p>



<a name="178251352"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/why%20does%20Any%20need%20%60%3A%20%27static%60%3F/near/178251352" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/why.20does.20Any.20need.20.60.3A.20&#x27;static.60.3F.html#178251352">(Oct 16 2019 at 01:40)</a>:</h4>
<p>Yes, any does not have any lifetimes for data contained behind that trait object, so this data must be 'static for any conversion to <code>Any</code> to be well-formed.</p>



<a name="178251404"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/why%20does%20Any%20need%20%60%3A%20%27static%60%3F/near/178251404" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/why.20does.20Any.20need.20.60.3A.20&#x27;static.60.3F.html#178251404">(Oct 16 2019 at 01:41)</a>:</h4>
<p>I find it easier to reason about this with closures that get passed to threads. They do similar kind of type erasure and it is easier to reason about why the requirement like that ends up being present (as threads may outlive the parent scope)</p>



<a name="178265648"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/why%20does%20Any%20need%20%60%3A%20%27static%60%3F/near/178265648" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/why.20does.20Any.20need.20.60.3A.20&#x27;static.60.3F.html#178265648">(Oct 16 2019 at 07:34)</a>:</h4>
<p>this question gets raised every now and then so I think it would be good to add some docs about it to Any</p>



<a name="178285290"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/why%20does%20Any%20need%20%60%3A%20%27static%60%3F/near/178285290" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> csmoe <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/why.20does.20Any.20need.20.60.3A.20&#x27;static.60.3F.html#178285290">(Oct 16 2019 at 12:55)</a>:</h4>
<blockquote>
<p>this question gets raised every now and then so I think it would be good to add some docs about it to Any</p>
</blockquote>
<p><span class="user-mention" data-user-id="126931">@centril</span>  and downcast stuff with <code>Box&lt;dyn Any&gt;</code> <a href="https://stackoverflow.com/questions/33687447/how-to-get-a-reference-to-a-concrete-type-from-a-trait-object" target="_blank" title="https://stackoverflow.com/questions/33687447/how-to-get-a-reference-to-a-concrete-type-from-a-trait-object">https://stackoverflow.com/questions/33687447/how-to-get-a-reference-to-a-concrete-type-from-a-trait-object</a> , it behaves unexpectedly to new comers.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>